//
//  ShareUIView.m
//  SMSTestDemo
//
//  Created by myApple on 16/2/4.
//  Copyright (c) 2016年 myApple. All rights reserved.
//

#import "ShareUIView.h"

@interface ShareUIView()
{
    NSArray * shareTextArray;
    UIButton * cancelBtn;
}

@end

@implementation ShareUIView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareView];
    }
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLanguage) name:@"changeLanguage"object:nil];
    return self;
}

#pragma mark ----------切换语言
-(void)changeLanguage
{
     shareTextArray = @[@"QQ",NSLocalizedString(@"weChatShare", nil),NSLocalizedString(@"messageShare", nil)];
    for (int i = 0 ; i < shareTextArray.count; i ++) {
        UILabel * iconLabel = (UILabel *)[self viewWithTag:(2255 + i)];
        iconLabel.text = shareTextArray[i];
    }
    [cancelBtn setTitle:NSLocalizedString(@"alterCancel", nil) forState:0];
}

-(void)prepareView
{
    //分享view
    UIView * shareUIView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 180, [UIScreen mainScreen].bounds.size.width, 180)];
    shareUIView.backgroundColor = [UIColor whiteColor];
    [self addSubview:shareUIView];
    
    //点击手势view
    UIView * touchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 180)];
    touchView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self addSubview:touchView];
    UITapGestureRecognizer * cancelTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelClick)];
    [touchView addGestureRecognizer:cancelTap];
    
    //图标布局
    NSArray * imageNameArray = @[@"qqShare",@"wechatShare",@"messageShare"];
    shareTextArray = @[@"QQ",NSLocalizedString(@"weChatShare", nil),NSLocalizedString(@"messageShare", nil)];
    float y = 20;
    float screenWidth = [UIScreen mainScreen].bounds.size.width;
    float width = 60;
    float height = 60;
    float offx = (screenWidth - 3 * width) / 4.0;
    for (int i = 0 ; i < 3; i ++) {
        UIButton * shareBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        shareBtn.frame = CGRectMake(offx + (width + offx) * i, y, width, height);
        [shareBtn setImage:[UIImage imageNamed:imageNameArray[i]] forState:0];
        [shareBtn addTarget:self action:@selector(clickShare:) forControlEvents:UIControlEventTouchUpInside];
        shareBtn.tag = 255 + i;
        [shareUIView addSubview:shareBtn];
        
        UILabel * shareLabel = [[UILabel alloc]initWithFrame:CGRectMake(shareBtn.frame.origin.x, shareBtn.frame.origin.y + shareBtn.frame.size.height + 10, width, 20)];
        shareLabel.text = shareTextArray[i];
        shareLabel.tag = 2255 + i;
        shareLabel.textAlignment = NSTextAlignmentCenter;
        shareLabel.font = [UIFont systemFontOfSize:14.0];
        [shareUIView addSubview:shareLabel];
    }
    
    y += height + 40;
    width = screenWidth - 2 * 20;
    height = 40;
    cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:NSLocalizedString(@"alterCancel", nil) forState:0];
    [cancelBtn setBackgroundColor:RGBA(125, 194, 25, 1)];
    cancelBtn.frame = CGRectMake(20, y, width, height);
    cancelBtn.layer.cornerRadius = 5;
    [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [shareUIView addSubview:cancelBtn];
    
}

#pragma mark -- 点击按钮
-(void)clickShare:(UIButton *)btn
{
    NSInteger index = 0;
    switch (btn.tag) {
        case 255:
            index = 0;
            break;
        case 256:
            index = 1;
            break;
        case 257:
            index = 2;
            break;
        default:
            break;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(shareClick:)]) {
        [_delegate shareClick:index];
    }
}

#pragma mark --- 点击取消
-(void)cancelClick
{
    if (_delegate && [_delegate respondsToSelector:@selector(cancelShare)]) {
        [_delegate cancelShare];
    }
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
